local jass = require 'jass.common'
local japi = require 'jass.japi'

local selector = {}
setmetatable(selector,selector)
ac.selector = selector 

-- 选取类型
selector.class = { }

function selector.init()
    require 'types.selector.player'
    require 'types.selector.unit'
    require 'types.selector.item'
    require 'types.selector.destructable'
	require 'types.selector.mover'
end

-- 创建选取器
function selector:__call(name)
    if not name or not selector.class[name] then 
        log.error('不存在的选取器类型',name)
        return 
    end
    return setmetatable({
		_filters = { },
	},selector.class[name])
end


local mt = { }
selector.__index = mt 

-- Jie
mt.type = 'selector'

-- 范围选取
-- @param point 中心
-- @param range 范围
function mt:range(point,range)
	self._type = 'range'
	self._point = point:get_point()
	self._range = range
	return self
end

-- 扇形选取
-- @param point 中心
-- @param range 范围
-- @param angle 角度
-- @param section 区间
function mt:sector(point,range,angle,section)
	self._type = 'sector'
	self._point = point:get_point()
	self._range = range
	self._angle = angle 
	self.section = section or 45.0
	return self
end 

-- 矩形选取
-- @param rect 区域
function mt:rect(rect)
	self._type = 'rect'
	self._rect = rect or ac.rect.map 
	return self
end

-- 直线选取
-- @param point 起点
-- @param angle 角度 
-- @param len 长度
-- @param width 宽度
function mt:line(point,angle,len,width)
	self._type = 'line'
	self._point = point:get_point()
	self._angle = angle 
	self._len = len 
	self._width = width
	return self
end 

-- 添加自定义条件
-- @param func 判定
function mt:filter(func)
	if func then 
		table.insert(self._filters,func)
	end
	return self
end 

-- 设定排序规则
-- @param func 判定
function mt:sorter(func)
	if func then 
		self._sorter = func 
	end 
	return self
end 

-- 不是
-- @param target 目标
function mt:is_not(target)
	return self:filter(function(dest)
		return dest ~= target
	end)
end

-- 筛选
function mt:do_filter(target)
	for i = 1, #self._filters do
		local func = self._filters[i]
		if not func(target) then
			return false
		end
	end
	return true
end 

-- 选取
function mt:select()
	return { }
end

-- 由近到远排序
function mt:near(dest)
	self:sorter(function(a,b)
		local r1 = a:get_point() * dest:get_point()
		local r2 = b:get_point() * dest:get_point()
		return r1 < r2
	end)
	return self
end

-- 由远到近
function mt:far(dest)
	self:sorter(function(a,b)
		local r1 = a:get_point() * dest:get_point()
		local r2 = b:get_point() * dest:get_point()
		return r1 > r2
	end)
	return self
end

-- 权重排序
function mt:priority(info)
	self:sorter(function(a,b)
		local r1 = info[a.handle] or 0
		local r2 = info[b.handle] or 0
		return r1 > r2
	end)
	return self
end

-- 获得选取目标
function mt:get(count)
	local info = self:select()
	if self._sorter then
		table.sort(info, self._sorter)
	end
    if count then
        return table.random_table(info,count)
    end
	return info
end

-- 遍历
function mt:loop()
	local info = self:get()
	local loop = 0
	return function()
		loop = loop + 1 
		local u = info[loop]
		if u then 
			return u 
		end
	end 
end	

-- 随机抽取
function mt:random()
	local info = self:get()
	if #info > 0 then
		return info[math.random(1, #info)]
	end
end

-- 取第一个
function mt:first()
	local info = self:get()
	if #info > 0 then
		return info[1]
	end
end

return selector